package com.aerotechnic.service.implementation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aerotechnic.dao.implementation.PlanningTemplateDao;
import com.aerotechnic.entities.Planning;
import com.aerotechnic.entities.PlanningTemplate;
import com.aerotechnic.service.interfaces.PlanningTemplateBoIn;

@Service("PlanningTemplateBo")
@Transactional
public class PlanningTemplateBo implements PlanningTemplateBoIn {

	@Autowired
	private PlanningTemplateDao planningTemplateDao;

	@Override
	public void save(PlanningTemplate myEntity) {
		// TODO Auto-generated method stub
		planningTemplateDao.save(myEntity);

	}

	@Override
	public void update(PlanningTemplate myEntity) {
		// TODO Auto-generated method stub
		planningTemplateDao.update(myEntity);

	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		planningTemplateDao.delete(id);

	}

	@Override
	public PlanningTemplate findByID(Long id) {
		// TODO Auto-generated method stub
		return planningTemplateDao.findByID(id);
	}

	@Override
	public List<PlanningTemplate> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PlanningTemplate findBestScenario(String typeCheck,
			String typeAvion, float charge2notExcede) {

		// array having PlanningTemplate with charge less than charge2notExcede
		List<PlanningTemplate> tempArr = planningTemplateDao
				.getPlTemplWithLessCharge(typeCheck, typeAvion,
						charge2notExcede);

		return tempArr.isEmpty()? null: getplTemplHavingMinCharge(tempArr);
	}

	private float getMingTAT(List<PlanningTemplate> tempArr) {
		float minTat = tempArr.get(0).getTAT();
		for (PlanningTemplate pt : tempArr) {
			if (pt.getTAT() < minTat) {
				minTat = pt.getTAT();
			}
		}
		return minTat;
	}

	private PlanningTemplate getplTemplHavingMinCharge(
			List<PlanningTemplate> tempArr) {

		float minTat = getMingTAT(tempArr);
		PlanningTemplate res = new PlanningTemplate();
		for (PlanningTemplate pt : tempArr) {
			if (pt.getTAT() == minTat) {
				res = pt;
				break;
			}
		}
		return res;
	}

	@Override
	public List<PlanningTemplate> getPlTemplWithLessCharge(String typeCheck,
			String typeAvion, float charge2notExcede) {
		return planningTemplateDao.getPlTemplWithLessCharge( typeCheck,
				 typeAvion,  charge2notExcede);
	}
}
